//
//  WBTabBarController.swift
//  新浪微博
//
//  Created by 陈飞宇 on 2020/9/24.
//

import UIKit

class WBTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpChildVc();
        setUpComposeButton();
        
        
    }
    
    
    /// portrait 竖屏 landscape横排
  override var supportedInterfaceOrientations: UIInterfaceOrientationMask
  {
    return .portrait;
  }
 
    
    @objc func clickComposeButton(){
        
        
        
    }
    
    //私有控件
    private lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    //设置私有按钮方法
    private func setUpComposeButton(){
        tabBar.addSubview(composeButton);
        //设置按钮位置
        let count = CGFloat(viewControllers!.count);
        let w =  tabBar.bounds.width  / count - 1 ;
        // 突然就改 dy - 10
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w , dy: 0)
        composeButton.addTarget(self, action:#selector(clickComposeButton), for: .touchUpInside)
    }
    
    
}

extension WBTabBarController
{
    private func setUpChildVc() {//设置所有子控制器
        let array = [
            ["clasName" : "WBHomeViewController", "title": "首页" ,"imageName": "home",
             "visitorInfo" : ["imageName": "" , "messagge": "关注一些人,回看有什么惊喜!!!!!!!"]
            ],
            
            ["clasName" : "WBMessageViewController", "title": "消息" ,"imageName": "message_center",
             "visitorInfo" : ["imageName": "visitordiscover_image_message" , "messagge": "登录后,评论你的消息都会再这里显示"]
            ],
            
            ["clasName" : "UIViewController",],
            
            ["clasName" : "WBDiscoverViewController", "title": "发现" ,"imageName": "discover",             "visitorInfo" : ["imageName": "visitordiscover_image_message" , "messagge": "登录后,最新最全微博尽在掌握"]
            ],
            
            ["clasName" : "WBProfileViewController", "title": "我" ,"imageName": "profile",
             "visitorInfo" : ["imageName": "visitordiscover_image_profile" , "messagge": "登录后个人微博都会在这里展示"]
            ],
            
        ];
        var arrayM = [UIViewController]();
        for dict  in array {
            arrayM.append(controller(dict: dict));
        }
        viewControllers = arrayM
    }
    
    
    /// 通过字典创建一个子控制器
    /// - Parameter dict: 信息字典
    /// - Returns: 子控制器
    private func controller(dict:[String: Any]) -> UIViewController{
        
        guard let clasName = dict["clasName"] as? String ,
              let title = dict["title"] as? String ,
              let imageName = dict["imageName"] as? String,
              let cls  =  NSClassFromString(Bundle.main.nameSpace + "." + clasName) as? WBBaseViewController.Type ,
              let visitorDict = dict["visitorInfo"] as? [String : String]
        else {
            return UIViewController();
        }
        //创建视图控制器
        let Vc  = cls.init();
        Vc.title = title;
        Vc.visitorInfo = visitorDict
        Vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        Vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        Vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.orange] , for: .highlighted)
        //item 文字大小 系统默认12号
//        Vc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.font : UIFont.systemFont(ofSize: 5) ] , for:.normal);
        let nav = WBNavigationController(rootViewController: Vc)
        return nav;
        
    }
}
